package datastructure.stack;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020-03-21
 */
public class Calculator {

    public static void main(String[] args) {
        // 表达式
        String expression = "7+2*6-4";
        // 创建两个栈 数栈和符号栈
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);
        // 扫描索引
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        // 存放每次扫描
        char ch = ' ';
        // 用于拼接多位数
        String keepNum = "";
        // 开始while循环扫描表达式
        while (true) {
            // 依此得到表达式的每一个字符
            ch = expression.substring(index, index + 1).charAt(0);
            // 判断ch是什么，然后相应处理
            if (operStack.isOper(ch)) {
                // 如果是运算符
                // 判断当前符号栈是否为空
                if (!operStack.isEmpty()) {
                    // 如果符号栈有操作符 ，就进行比较，如果当前的操作符的优先级小于或等于栈中的操作符，就需要从数栈中pop出两个数
                    // 在从符号栈中pop出一个符号，进行运算，将结果入数栈，然后将当前符号入符号栈
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = operStack.cal(num1, num2, oper);
                        // 把运算结果入数栈
                        numStack.push(res);
                        // 把运算符入符号栈
                        operStack.push(ch);
                    } else {
                        // 如果当前的运算符优先级大于栈中的运算符，就直接入符号栈
                        operStack.push(ch);
                    }
                } else {
                    // 如果符号栈为空直接入符号栈
                    operStack.push(ch);
                }
            } else {
                // 如果是数值，则直接入数栈
                // 处理多位数
                keepNum += ch;
                // 如果ch已经是表达式最后一位，直接入数栈
                if (index == expression.length() - 1) {
                    numStack.push(Integer.parseInt(keepNum));
                } else {
                    // 判断是否是数字，如果是 继续扫描，否则入符号栈
                    if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        numStack.push(Integer.parseInt(keepNum));
                        // 让keepNum 清空
                        keepNum = "";
                    }
                }
            }

            index++;
            if (index >= expression.length()) {
                break;
            }
        }
        // 当表达式扫描完毕，就顺序从 数栈和符号栈pop出相应的数和符号，并运算
        while (!operStack.isEmpty()) {
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = operStack.cal(num1, num2, oper);
            numStack.push(res);
        }
        // 最后数栈中最后一个数就是计算结果
        int result = numStack.pop();
        System.out.println("表达式" + expression + "的值：" + result);
    }
}

class ArrayStack2 {
    private int maxSize = 100;
    private int top = -1;
    private int[] stack;

    public ArrayStack2(int maxSize) {
        if (maxSize < 0) {
            throw new RuntimeException("初始化长度参数错误");
        }
        if (maxSize > this.maxSize) {
            stack = new int[this.maxSize];
        } else {
            stack = new int[maxSize];
        }
    }

    /**
     * 入栈
     *
     * @param n 入栈数
     */
    public void push(int n) {
        if (isFull()) {
            System.out.println("栈满~~~~");
            return;
        }
        top++;
        stack[top] = n;
    }

    /**
     * 出栈
     *
     * @return 出栈数
     */
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("空栈~~~~");
        }
        int val = stack[top];
        top--;
        return val;
    }

    /**
     * 判断栈是否是空栈
     */
    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * 判断栈满
     */
    public boolean isFull() {
        return top >= maxSize - 1;
    }

    /**
     * 查看栈顶
     *
     * @return 栈顶元素
     */
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("空栈~~~~");
        }
        return stack[top];
    }

    /**
     * 遍历
     */
    public void showStack() {
        if (isEmpty()) {
            System.out.println("空栈~~~~");
            return;
        }
        for (int i = top; top > -1; top--) {
            System.out.println(stack[i]);
        }
    }

    /**
     * 获取运算符的优先级
     *
     * @param oper 运算符
     * @return 优先级
     */
    public int priority(int oper) {

        if (oper == '+' || oper == '-') {
            return 0;
        }
        if (oper == '*' || oper == '/') {
            return 1;
        }
        return -1;
    }

    /**
     * 判断是否是运算符
     *
     * @param val 运算符
     */
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    /**
     * 计算
     */
    public int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+':
                res = num2 + num1;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num2 * num1;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }
}